{include file="common/head"/}
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>传感器{:lang('list')}</legend>
    </fieldset>
    <div class="demoTable">
        <div class="layui-inline">
            <input class="layui-input" name="key" id="key" placeholder="输入传感器编码 传感器名称">
        </div>
        <button class="layui-btn" id="search" data-type="reload">搜索</button>
        <a href="javascript:location.replace(location.href);" class="layui-btn">刷新</a>
        <a class="layui-btn" style="margin-right: 0px;float: right;" id="add_dev" data-type="reload"
            onclick="SensorAdd()">添加传感器</a>
        <div style="clear: both;"></div>
    </div>
    <table class="layui-table" id="list" lay-filter="list"></table>
</div>
<script type="text/html" id="is_open">
    <input type="checkbox" name="is_open" value="{{d.id}}" lay-skin="switch" lay-text="开启|禁用" lay-filter="is_open" {{ d.is_open == 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="operation">
    <a class="layui-btn layui-btn-normal layui-btn-xs" onclick="devEdit('{{d.id}}')" >编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/html" id="img">
    <img src="{{d.img}}" height="50px;" width="50px"
         onmouseover="layer.tips('<img   src={{d.img}}>',this,{tips: [1, '#fff'],area:'200px,200px'});"  onmouseout="layer.closeAll();">
</script>
{include file="common/foot"/}
<script src="/static/common/js/jquery.2.1.1.min.js"></script>
<script>
    //添加传感器
    function SensorAdd() {
        layer.open({
            type: 2,
            title: '添加传感器',
            shade: 0.01,
            shadeClose: true,
            scrollbar: false,
            area: ['800px', '600px'],
            anim: 5,
            content: "{:url('SensorAdd')}"
        });
    }


    /**
     * 编辑
     */
    function devEdit(id) {
        layer.open({
            type: 2,
            title: '传感器编辑',
            shade: 0.01,
            shadeClose: true,
            scrollbar: false,
            area: ['800px', '600px'],
            anim: 5,
            content: "{:url('SensorEdit')}?id=" + id
        });
    }
</script>
<script>
    layui.use(['table', 'form'], function () {
        // 列表
        var table = layui.table, form = layui.form, $ = layui.jquery;
        var tableIn = table.render({
            id: 'user',
            elem: '#list',
            url: '{:url("SensorList")}',
            method: 'post',
            page: {
                curr: location.hash.replace('#!sensor=', '') 
                , hash: 'sensor'
            },
            cols: [[
                { field: 'id', title: '序号', width: 60, fixed: true, align: 'center' }
                , { field: 'sensor_mac', title: '传感器编码', width: 150, align: 'center' }
                , { field: 'sensor_name', title: '传感器名称', width: 150, align: 'center' }
                , { field: 'name', title: '传感器类型名称', width: 150, align: 'center' }
                , { field: 'range_min', title: '最小范围', width: 90, align: 'center' }
                , { field: 'range_max', title: '最大范围', width: 90, align: 'center' }
                , { field: 'gateway_id', title: '关联网关编号', width: 150, align: 'center' }
                , { field: 'gateway_name', title: '关联网关名称', width: 150, align: 'center' }
                , { field: 'house_name', title: '大棚名称', width: 180, align: 'center' }
                , { field: 'img', title: '传感器图片', width: 120, align: 'center', toolbar: '#img' }
                , { field: 'is_bad', title: '是否故障', width: 100, align: 'center' }
                , { field: 'is_open', title: '{:lang("status")}', width: 100, toolbar: '#is_open', align: 'center' },
                { field: 'is_del', align: 'center', title: '操作', toolbar: ' #operation ' }
            ]],
            limit: 10 //每页默认显示的数量
        });

        //状态开关
        form.on('switch(is_open)', function (obj) {
            loading = layer.load(1, { shade: [0.1, '#fff'] });
            var id = this.value;
            var is_open = obj.elem.checked === true ? 1 : 2;
            $.post('{:url("SensorState")}', { 'id': id, 'is_open': is_open }, function (res) {
                layer.close(loading);
                console.log(res);
                if (res.code === 1) {
                    layer.msg(res.msg, { time: 1000, icon: 1 });
                    tableIn.reload();
                } else {
                    layer.msg(res.msg, { time: 1000, icon: 2 });
                    return false;
                }
            })
        });
        table.on('tool(list)', function (obj) {
            var data = obj.data;
            //操作   删除
            if (obj.event === 'del') {
                layer.confirm('确定删除吗？', function (index) {
                    $.post("{:url('SensorDel')}", { id: data.id }, function (res) {
                        console.log(res);
                        if (res.code === 1) {
                            layer.msg(res.msg, { time: 1000, icon: 1 });
                            tableIn.reload();
                        } else {
                            layer.msg(res.msg, { time: 1000, icon: 2 });
                        }
                    });
                    layer.close(index);
                })
            }

        });
        //搜索
        $('#search').on('click', function () {
            var key = $('#key').val();
            if ($.trim(key) === '') {
                layer.msg('{:lang("pleaseEnter")}关键字！', { icon: 0 });
                return;
            }
            tableIn.reload({
                where: { key: key }
            });
        });

    });

</script>
</body>

</html>